﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;

namespace Common.Tests.System.Threading.Tasks
{
    [TestFixture]
    public class AsyncLazyTests
    {

        [Test]
        public async void CanConstruct_using_func_of_T()
        {
            const string expectedString = "This is a test string!";
            var target = new AsyncLazy<string>(() =>
                                               {
                                                   Thread.Sleep(300);
                                                   return expectedString;
                                               });

            var result = await target;
            Assert.AreEqual(expectedString, result);
        }

        [Test]
        public async void CanConstruct_using_factory_of_T()
        {
            const string expectedString = "This is a test string!";
            var target = new AsyncLazy<string>(() => Task.Run(() =>
                                                                      {
                                                                          Thread.Sleep(300);
                                                                          return expectedString;
                                                                      }));

            var result = await target;
            Assert.AreEqual(expectedString, result);
        }
    }
}
